package com.hd.trans.share;

import android.util.Log;

import com.hd.trans.R;
import com.hd.trans.ui.base.AppContextProvider;
import com.hd.trans.utils.FileUtils;

import java.io.File;

import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;

public class TextToPdfModel {
    private final String TAG = "TextToPdfModel";

    private TextToPdfRequest textToPdfRequest = null;


    /**
     * 将读取到的文本内容 提交转PDF接口
     *
     * @param param
     * @param saveName
     * @param callback
     */
    public void requestTextToPdf(TextToPdfParam param, String saveName, TextToPdfModelCallback callback) {
        if (textToPdfRequest == null) {
            textToPdfRequest = new TextToPdfRequest();
        }
        textToPdfRequest.postRequest(param).subscribe(new Observer<TextToPdfBean>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(TextToPdfBean textToPdfBean) {
                Log.d(TAG, "requestTextToPdf成功拿到下载路径-------->" + textToPdfBean.toString());
                // 下载文件
                downloadFile(textToPdfBean, saveName, callback);
            }

            @Override
            public void onError(Throwable e) {
                Log.d(TAG, "requestTextToPdf成失败-------->" + e);
                if (callback != null) callback.onTextToPdfModelError(e.getMessage());
            }

            @Override
            public void onComplete() {

            }
        });
    }

    /**
     * 最后下载 已经转换pdf文件
     *
     * @param bean
     * @param sveName
     * @param callback
     */
    private void downloadFile(TextToPdfBean bean, String sveName, TextToPdfModelCallback callback) {
        String docFilePath = FileConfigManager.getCacheDirSharePath() + File.separator + sveName;
        // 删除缓存的文件
        FileUtils.clearFolderSubFiles(new File(docFilePath).getParentFile());
        UrlDownloadRequest downloadThread = new UrlDownloadRequest(null, bean.getDownurl(), docFilePath);
        downloadThread.request(new UrlDownloadRequest.DownloadCallback() {
            @Override
            public void onDownloadStart(DownloadArg downloadArg) {
                Log.d(TAG, "onDownloadStart--------------->");
                callback.onTextToPdfModelStart();
            }

            @Override
            public void onDownloadProgress(DownloadArg downloadArg, int progress) {
                Log.d(TAG, "onDownloadProgress------------progress--->" + progress);
                callback.onTextToPdfModelProgress(progress);
            }

            @Override
            public void onDownloadError(DownloadArg downloadArg) {
                Log.d(TAG, "onDownloadError--------------->");
                callback.onTextToPdfModelError(AppContextProvider.getContext().getString(R.string.download_fail));
            }

            @Override
            public void onDownloadComplete(DownloadArg downloadArg) {
                Log.d(TAG, "onDownloadComplete--------------->");
                callback.onTextToPdfModelComplete(downloadArg);
            }
        });
    }


    public interface TextToPdfModelCallback {
        // 显示开始
        void onTextToPdfModelStart();

        // 显示当前进度
        void onTextToPdfModelProgress(int progress);

        // 显示出错
        void onTextToPdfModelError(String msg);

        // 结束
        void onTextToPdfModelComplete(DownloadArg downloadArg);
    }
}
